//
//  SwitchLatestViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/25.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxSwift

class SwitchLatestViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addTapWithHandle { (tap) in
            
            let disBag = DisposeBag()
            
            let subject1 = BehaviorSubject(value: "11")
            let subject2 = BehaviorSubject(value: "21")
            
            let variable = Variable(subject1)
            
            
            // 每当一个新的序列发射时，原来序列将被丢弃
            variable.asObservable()
                .switchLatest()
                .subscribe({ (eve) in
                    print(eve)
                })
                .disposed(by: disBag)
            
            subject1.onNext("12")
            subject1.onNext("13")
            
            variable.value = subject2
            
            subject1.onNext("14")
            subject1.onNext("15")
            
            subject2.onNext("22")
            subject2.onNext("23")
            
        }
        
    }
}
